#include "List.cpp"

//给你一个链表，删除链表的倒数第 n 个结点，并且返回链表的头结点。

//进阶：你能尝试使用一趟扫描实现吗？

ListNode* removeNthFromEnd(ListNode* head, int n) {
    ListNode* newHead = (ListNode*) malloc(sizeof(ListNode));
    newHead->next = head;

    ListNode* p = newHead;
    for (int i = 0; i <n;  ++i) {
        p = p->next;
    }

    ListNode* pre = newHead;
    while (p->next != NULL) {
        p = p->next;
        pre = pre->next;
    }
    pre->next = pre->next->next;

    return newHead->next;
}

int main() {
    int arr[] = {1,2,3,4,5};
    ListNode* head = CreateListByArray(arr, ARRAY_SIZE(arr));
    head = removeNthFromEnd(head,2);
}
